import time
import logging

from drf_shop_admin.settings import dev as settings
from jose import jwt

logger = logging.getLogger('error')

class JwtUtil:
    @staticmethod
    def gen_jwt_token(user: dict):
        expire_time = {
            'exp': time.time() + settings.REDIS_TIMEOUT
        }
        user.update(expire_time)
        token = jwt.encode(user, settings.SECRET_KEY, algorithm=settings.ALGORITHM)

        return token

    @staticmethod
    def check_jwt_token(token):
        payload = {}
        try:
            payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
        except Exception as e:
            logger.error(e)

        return payload
